/*
 * @lc app=leetcode.cn id=202 lang=cpp
 *
 * [202] 快乐数
 */

// @lc code=start
class Solution
{
public:
    int getsum(int n)
    {
        int sum = 0;
        while (n)
        {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n)
    {
        //这道题的重点就在于 如果sum曾经出现过那么就会无限循环 不会为1
        //所以将sum存set如果找到了就返回false
        unordered_set<int> happy;
        while (1)
        {
            int sum = getsum(n);
            if (sum == 1)
            {
                return true;
            }
            else
            {
                if (happy.find(sum) == happy.end())
                {
                    happy.emplace(sum);
                }
                else
                {
                    return false;
                }
            }
            n = sum;
        }
        return false;
    }
};
// @lc code=end
